﻿using AutoMapper;
using Common;
using DB.Model;
using DB.Model.Input;
using DB.Model.OutPut;
using DB.MongoDB;
using DB.MongoDB.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NoticeWechat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NoticeWechatApi.Controllers
{
    [Authorize]
    public class TasksController : ApiController
    {
        IMongoDbHelper<Tasks> _Db; IMapper _mapper; IMongoDbHelper<User> _uDb;
        public TasksController(IMongoDbHelper<Tasks> mongoDb, IMapper mapper, IMongoDbHelper<User> uDb)
        {
            _Db = mongoDb; _mapper = mapper;
            _uDb = uDb;
        }

        [AllowAnonymous]
        [HttpPost]
        public async Task<AjaxResult> PostMessage(PostMsg tasksInput)
        {
            if (tasksInput.UserId.IsNullOrEmpty()) return new AjaxResult() { Message = "失败", Type = ResultType.Failed };
            if (_uDb.QueryOne(tasksInput.UserId) == null) return new AjaxResult() { Message = "失败", Type = ResultType.Failed };
            var tasks = _mapper.Map<Tasks>(tasksInput);
            tasks.StartDate = tasks.FirstDate.Date.ToString();
            tasks.StartHour = tasks.FirstDate.Hour;
            tasks.StartMinute = tasks.FirstDate.Minute;
            tasks.TaskStartType = 0;
            tasks.TaskType = 0;
            tasks.Status = 0;
            await _Db.Insert(tasks);
            return new AjaxResult() { Message = "新增任务成功", Type = ResultType.Success };
        }

        [HttpPost]
        public async Task<AjaxResult> Insert(TasksInput tasksInput)
        {
            var tasks = _mapper.Map<Tasks>(tasksInput);
            tasks.UserId = User.Identity.GetUserId();
            tasks.Status = 0;
            await _Db.Insert(tasks);
            return new AjaxResult() { Message = "新增成功", Type = ResultType.Success };
        }

        [HttpPost]
        public async Task<AjaxResult> Update(TasksInput tasksInput)
        {
            var tasks = _mapper.Map<Tasks>(tasksInput);
            tasks.Id = tasksInput.Uid.ToObjectId();
            tasks.UserId = User.Identity.GetUserId();
            await _Db.Update(tasks);
            return new AjaxResult() { Message = "修改成功", Type = ResultType.Success };
        }

        [HttpPost]
        public async Task<AjaxResult> Delete(string id)
        {
            await _Db.Delete(id);
            return new AjaxResult() { Message = "删除成功", Type = ResultType.Success };
        }

        [HttpGet]
        public async Task<Tasks> QueryOne(string id)
        {
            return await _Db.QueryOne(id);
        }

        [HttpPost]
        public async Task<List<TasksOutPut>> QueryAll(FilterGroup tasksInput)
        {
            var id = User.Identity.GetUserId();
            tasksInput.FilterRules.Add(new FilterRule("UserId", id));
            var result = await _Db.QueryFilter(tasksInput);
            var list = _mapper.ProjectTo<TasksOutPut>(result.AsQueryable());
            return list.ToList();
        }
    }
}
